package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 订单
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.sumbit(orders);
        return R.success("下单成功！");
    }

    /**
     * 查询订单
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        //构造分页构造器
        Page<Orders> pageInfo = new Page(page, pageSize);
        Page<OrdersDto> orderPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //执行查询
        orderService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,orderPage,"records");
        //根据id返回购物车
        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> list = records.stream().map((item)-> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);//拷贝

            LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper();
            lqw.eq(OrderDetail::getOrderId,item.getId());

            List<OrderDetail> orderDetailList = orderDetailService.list(lqw);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());
            orderPage.setRecords(list);

        return R.success(orderPage);

    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number,String beginTime,String endTime){
        log.info("在这返回数据{},{}",number,beginTime);
        Page<Orders> pageInfo = new Page(page, pageSize);
        Page<OrdersDto> orderPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        if (number != null) {
            queryWrapper.like(Orders::getNumber, number);
        }
        if (beginTime != null){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime beginTimex = LocalDateTime.parse(beginTime,df);
            LocalDateTime endTimex = LocalDateTime.parse(endTime,df);
            queryWrapper.ge(Orders::getOrderTime,beginTimex).le(Orders::getOrderTime,endTimex);
        }
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //执行查询
        orderService.page(pageInfo,queryWrapper);

        //拷贝
        BeanUtils.copyProperties(pageInfo,orderPage,"records");
        //根据id返回购物车
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item)-> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);//拷贝
            ordersDto.setUserName(item.getConsignee());//联系人
            return ordersDto;
        }).collect(Collectors.toList());
        orderPage.setRecords(list);
        return R.success(orderPage);
    }

    /**
     * 修改状态
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("此单已经修改完成");
    }


}
